/**
 * 
 */
package jpb;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * Map decorator that does not allow <code>null</code> keys or values.
 * 
 * @author Dariusz Kordonski
 */
final class StrictMap<K,V> implements Map<K,V> {

    private final Map<K,V> delegate;
    
    public StrictMap(Map<K,V> delegate) {
        this.delegate = delegate;
    }
    
    @Override
    public void clear() {
        delegate.clear();
    }

    @Override
    public boolean containsKey(Object key) {
        return delegate.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return delegate.containsValue(value);
    }

    @Override
    public Set<Entry<K,V>> entrySet() {
        return delegate.entrySet();
    }

    @Override
    public V get(Object key) {
        return delegate.get(key);
    }

    @Override
    public boolean isEmpty() {
        return delegate.isEmpty();
    }

    @Override
    public Set<K> keySet() {
        return delegate.keySet();
    }

    @Override
    public V put(K key, V value) {
        validate(key, value);
        return delegate.put(key, value);
    }

    @Override
    public void putAll(Map<? extends K,? extends V> m) {
        for (Entry<? extends K,? extends V> en : m.entrySet()) {
            validate(en.getKey(), en.getValue());
        }
        delegate.putAll(m);
    }

    @Override
    public V remove(Object key) {
        return delegate.remove(key);
    }

    @Override
    public int size() {
        return delegate.size();
    }

    @Override
    public Collection<V> values() {
        return delegate.values();
    }

    private void validate(K key, V value) {
        Assert.notNull("key", key);
        Assert.notNull("value", value);
    }
}
